package command

import (
	"fmt"
	"strconv"
	"strings"
	"willow/cachestring"
	"willow/common/conf"
	"willow/common/logs"
)

func init()  {
	sdsInit()
}

var command []string

func Parameter(str string , saveBool bool) int8 {
	commdTemp := strings.Split(strings.TrimSpace(str)," ")
	sdsString := &cachestring.SdsString{}
	list      := &cachestring.WList{}
	switch strings.ToLower(commdTemp[0]) {
	case command[0]:
		if len(commdTemp) == 3 {
			val := sdsString.SetValue(commdTemp[1],commdTemp[2])
			if val == 1 && saveBool{
				fmt.Println("set success")
				return 1
			} else if saveBool {
				fmt.Println("set serror")
				return -1
			}
		}
	case command[1]:
		if len(commdTemp) == 2 {
			val := sdsString.GetValue(commdTemp[1])
			if saveBool {
				if val == "" {
					fmt.Println("the key is not exists")
				} else {
					fmt.Println(val)
				}

			}
			return 1
		}
	case command[2]:
		if  len(commdTemp) > 1 && len(commdTemp)%2 == 1  {
			tempMset := [][2]string{}
			for i := 1; i <= len(commdTemp)/2 ; i++  {
				temp := [2]string{}
				temp[0] = commdTemp[i*2-1]
				temp[1] = commdTemp[i*2]
				tempMset = append(tempMset,temp)
			}
			sdsString.MSet(tempMset)
			if saveBool {
				fmt.Println("mset success")
			}
			return 1
		}
	case command[3]:
		if conf.WillowConf["appendonlyC"] == "aof" {
			go sdsString.SaveString()
			return 1
		} else {
			sdsString.SaveStringRDB()
			return 1
		}
	case command[4]:
		if len(commdTemp) == 3 {
			val := sdsString.SetnxValue(commdTemp[1],commdTemp[2])
			if val == 1 && saveBool{
				fmt.Println("setnx success")
				return 1
			} else if saveBool {
				fmt.Println("the key already exists")
				return -2
			}
		}
	case command[5]:
		if  len(commdTemp) > 1 && len(commdTemp)%2 == 1 {
			tempMset := [][2]string{}
			for i := 1; i <= len(commdTemp)/2 ; i++  {
				temp := [2]string{}
				temp[0] = commdTemp[i*2-1]
				temp[1] = commdTemp[i*2]
				tempMset = append(tempMset,temp)
			}
			total :=  sdsString.MSetnx(tempMset)
			if  saveBool && total == 1 {
				fmt.Println("msetnx success")
			} else if saveBool && total == -2 {
				fmt.Println("the key already exists")
			}
			return int8(total)
		}
	case command[6]:
		if  len(commdTemp) == 3  {
			valNum, err := strconv.ParseInt(commdTemp[2],10,0)
			if err != nil {
				logs.ErrorLog(strconv.Itoa(int(valNum)))
			}
			val := sdsString.SetOutTime(commdTemp[1],valNum)
			if val == 1 && saveBool{
				fmt.Println("setnx success")
				return 1
			} else if saveBool {
				fmt.Println("the key is not exist")
				return -2
			}
		}
	case command[7]:
		if  len(commdTemp) == 2  {
			val := sdsString.GetOutTime(commdTemp[1])
			if val != 1 && saveBool{
				fmt.Println(val)
				return 1
			} else if saveBool {
				fmt.Println("the key is not exist")
				return -2
			}
		}
	case command[8]:
		if  len(commdTemp) == 2 {
			sdsString.DeleteValue(commdTemp[1])
			if saveBool {
				fmt.Println("del success")
			}
			return 1
		}
	case command[9]:
		if  len(commdTemp) == 2 {
			sdsString.DeleteSafeValue(commdTemp[1])
			if saveBool {
				fmt.Println("delsafe success")
			}
			return 1
		}
	case command[10]:
		if  len(commdTemp) == 2 {
			val := sdsString.GetLen(commdTemp[1])
			if saveBool && val != -1 {
				fmt.Println(val)
				return 1
			} else if !saveBool && val != -1 {
				return 1
			} else {
				fmt.Println("the key is not exist")
				return -2
			}
		}
	case command[11]:
		if  len(commdTemp) == 2 {
			val := sdsString.Incr(commdTemp[1])
			if saveBool && val == nil {
				fmt.Println("incr success")
				return 1
			} else if !saveBool && val == nil {
				return 1
			} else if saveBool && val != nil  {
				fmt.Println(val)
				return -2
			}else if !saveBool && val != nil{
				return -2
			}
		}
	case command[12]:
		if  len(commdTemp) == 3 {
			val := list.LPush(commdTemp[1],commdTemp[2])
			if saveBool && val != -1 {
				fmt.Println(val)
				return 1
			} else if saveBool && val == -1 {
				return -1
			} else if !saveBool {
				return -2
			}
		}
	case command[13]:
		if  len(commdTemp) == 2 {
			val := list.BLPop(commdTemp[1])
			if saveBool && val != nil {
				fmt.Println(val)
				return 1
			} else if saveBool && val == nil {
				return -1
			} else if !saveBool {
				return -2
			}
		}
	case command[14]:
		if  len(commdTemp) == 2 {
			val := list.BLPop(commdTemp[1])
			if saveBool && val != nil {
				fmt.Println(val)
				return 1
			} else if saveBool && val == nil {
				return -1
			} else if !saveBool {
				return -2
			}
		}
	}
	return -1
}

func sdsInit()  {
	command = []string{
		"set",
		"get",
		"mset",
		"save",
		"setnx",
		"msetnx",
		"setex",
		"getex",
		"del",
		"delsafe",
		"getlen",
		"incr",
		"lpush",
		"blpop",
		"brpop",
	}
}
